var mineModelTree = new eiTreeModel('ZMCB01');
var selectNode;
var parentNode;
var rootNode;
//启动事件处理
$(document).ready(function() {
	efregion.show('ef_region_inqu');
	$("#_left").resizable({
		handles: "e",
		 iframeFix: true,  
		 stop: function( event, ui ) {
			 $("#_right").css("margin-left",  $("#_left").outerWidth());
		 }
	});
});

efform_onload = function ()
{
	$('#ef_region_inqu1').hide();
	$("#mineTree").css("height", $(".ef-layout").outerHeight());
	var mineID=$('#fdKeyId').val();
	var mineName=$('#fdMiningName').val();
	ef.get("_right_iframe").src="DispatchAction.do?efFormEname=ZMCB02&fdParentId="+mineID+"_0&fdMineId="+mineID+"_0&display="+mineName+"&level=0&fdParentType=矿井&isLeaf=0"; 
}; 
 
function configTree(tree) {
	tree.emptyNodeAsLeaf = false;
	tree.activeEmptyJudger = false;
	tree.topNodeActive = true;
	tree.nodeInitializer = treeNodeInitializer;
  	tree.initialExpandDepth = 2;
  	tree.hideRoot = false;
  	tree._rootNode.active(true);
  	tree.expand();
	expandNode(tree);
}
function expandNode(parent) {
	var childNodes = parent.getChildNodes();
	for (var i = 0; i < childNodes.length; i++) {
		childNodes[i].expand();
	}
}
function reloadCurrentNode()
{
	if(parentNode!=null)parentNode.reload();
	rootNode.reload();
}
function treeNodeInitializer(node) {
	
	if ( node.top() )
	{ 
		rootNode=node;
	    node.active(true);
	    node.textClicked = function(){ treeNodeClicked( node ); };
	    return;
	}  
    node.textClicked = function(){ treeNodeClicked( node ); };
 
}
function treeNodeClicked(node) {
	selectNode = node;
	parentNode=node.parent();
	var _value=$('#value').val();
	//alert("value:"+_value+"\nlabel:"+node.data().label+"\ntext:"+node.data().text+"\ntype:"+node.data().type+"\nleaf:"+node.data().leaf+"\nlevel:"+node.data().level);
	var level = node.data().level;
	var args=getTreeNodeInfo(node);
	if (_value=="0")
    {
        switch (level)
        {
            case "0":
                //点击矿井显示 水平+巷道
        	 	ef.get("_right_iframe").src="DispatchAction.do?efFormEname=ZMCB02&"+args; 
                break;
            case "1":
                //点击水平显示 采区+巷道
        	 	ef.get("_right_iframe").src="DispatchAction.do?efFormEname=ZMCB03&"+args; 
                break;
            case "2":
                //点击采区显示 采面+巷道
        	 	ef.get("_right_iframe").src="DispatchAction.do?efFormEname=ZMCB04&"+args; 
                break;
            case "3":
                //点击采面显示 巷道
        	 	ef.get("_right_iframe").src="DispatchAction.do?efFormEname=ZMCB05&"+args; 
                break;
            case "4":
                //点击巷道显示 巷道明细
        	 	ef.get("_right_iframe").src="DispatchAction.do?efFormEname=ZMCB07&"+args; 
                break;
        }
    }
    else if (_value=="1")
    {
         switch (level)
         {
         	case "0":
                //采区
        	 	ef.get("_right_iframe").src="DispatchAction.do?efFormEname=ZMCB03&"+args; 
                break;
            case "2":
                //采面
        	 	ef.get("_right_iframe").src="DispatchAction.do?efFormEname=ZMCB04&"+args; 
                break;
            case "3":
                //巷道
        	 	ef.get("_right_iframe").src="DispatchAction.do?efFormEname=ZMCB05&"+args; 
                break;
            case "4":
                //巷道
        	 	ef.get("_right_iframe").src="DispatchAction.do?efFormEname=ZMCB07&"+args; 
                break;
        }
    }
    else
    {
        switch (level)
        {
            case "0":
                //采面
        	 	ef.get("_right_iframe").src="DispatchAction.do?efFormEname=ZMCB04&"+args; 
                break;
            case "3":
                //巷道
            	ef.get("_right_iframe").src="DispatchAction.do?efFormEname=ZMCB05&"+args; 
                break;
            case "4":
                //巷道
        	 	ef.get("_right_iframe").src="DispatchAction.do?efFormEname=ZMCB07&"+args; 
                break;
        }
    }
	$('#_right_iframe').show();
	$('#ef_region_inqu1').hide();
}
function getTreeNodeInfo(node)
{
    var value = node.data().label;  
    var display = node.data().text; 
    var type = node.data().type; 
    var level = node.data().level; 
    var isLeaf = node.data().leaf; 
    var args="fdParentId="+value+"&fdMineId="+value+"&display="+display+"&level="+level+"&fdParentType="+type+"&isLeaf="+isLeaf;
//    alert(args);
    return args;
}
button_update_onclick = function (){ 
	//根据节点标识查询数据以展示在页面上
	var _info = new EiInfo();
	if(typeof(selectNode)=="undefined"){
		EFAlert("请选择矿井节点再进行修改");
		return;
	}
	if(selectNode.data()==null||selectNode.data().level!=0){
		EFAlert("只能修改矿井节点");
		return;
	}
	_info.set("fdKeyId",selectNode.data().label.substring(0,selectNode.data().label.indexOf("_")));
	EiCommunicator.send("ZMCB01", "getMineByID", _info, ajax_callback1);
}
var ajax_callback1 = {
		onSuccess : function(eiInfo) {
			var block = eiInfo.getBlock("result");  
			$('#treeadd-0-fdKeyId').val(block.getCell(0,"fdKeyId"));
			$('#treeadd-0-fdMiningId').val(block.getCell(0,"fdMiningId"));
			$('#treeadd-0-fdStatus').val(block.getCell(0,"fdStatus"));
			$('#treeadd-0-fdDepartmentId').val(block.getCell(0,"fdDepartmentId"));
			$('#treeadd-0-fdNo').val(block.getCell(0,"fdNo"));
			$('#treeadd-0-fdName').val(block.getCell(0,"fdName"));
			$('#treeadd-0-fdReserves').val(block.getCell(0,"fdReserves"));
			$('#treeadd-0-fdRecoverable').val(block.getCell(0,"fdRecoverable"));
			$('#treeadd-0-fdIndustrial').val(block.getCell(0,"fdIndustrial"));
			$('#treeadd-0-fdManufacturability').val(block.getCell(0,"fdManufacturability"));
			$('#treeadd-0-fdAbility').val(block.getCell(0,"fdAbility"));
			$('#treeadd-0-fdServiceLength').val(block.getCell(0,"fdServiceLength"));
			$('#treeadd-0-fdMineLegnth').val(block.getCell(0,"fdMineLegnth"));
			$('#treeadd-0-fdTiltLength').val(block.getCell(0,"fdTiltLength"));
			$('#treeadd-0-fdMineArea').val(block.getCell(0,"fdMineArea"));
			
			efwindow.hide(); 
			$('#_right_iframe').hide();
			$('#ef_region_inqu1').show();
			efbutton.setButtonStatus("update", true);
		},
		onFail : function(eMsg) {
		}
	};

button_insert_onclick = function (){ 
	$('#treeadd-0-fdMiningId').val($('#fdMiningId').val());
	$('#treeadd-0-fdName').val($('#fdMiningName').val());
	$('#_right_iframe').hide();
	$('#ef_region_inqu1').show();
	efbutton.setButtonStatus("insert", true);
}
button_delete_onclick = function (){ 
	var fdKeyId=selectNode.data().label; 
	var info = new EiInfo();
	info.set("fdKeyId", fdKeyId);
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		EiCommunicator.send("ZMCB01", "delete", info, save_ajax_callback);
	});
}
button_save_onclick = function (){ 
	if(efvalidateDiv("ef_region_inqu1")){ 
		var info = getDetail("ef_region_inqu1","treeadd");
		if (null != info) {
			if($('#ef_region_inqu1').find('#treeadd-0-fdKeyId').val())
			{
				EiCommunicator.send("ZMCB01", "update", info, save_ajax_callback);
			}else
			{
				EiCommunicator.send("ZMCB01", "insert", info, save_ajax_callback);
			}
		} 
	}
}
var save_ajax_callback= {
	onSuccess: function(eiInfo) { 
		reloadCurrentNode();
	},
	onFail:function(eMsg)
	{
		EFAlert("操作失败，原因："+eMsg); 
	}
}
button_cancel_onclick = function (){ 
	$('#_right_iframe').show();
	$('#ef_region_inqu1').hide();
} 
 